package com.chris.homecare.components
{
	import com.chris.homecare.TextureAtlasManager;
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.Button;
	import feathers.controls.Header;
	import feathers.controls.LayoutGroup;
	
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.events.Event;
	import starling.textures.Texture;

	[Event(name="triggerBackBtn",type="starling.events.Event")]
	[Event(name="triggerActionSheet",type="starling.events.Event")]
	
	public class HeaderComponent extends LayoutGroup
	{
		public static const TRIGGER_BACK_BTN:String = "triggerBackBtn";
		public static const TRIGGER_ACTIONSHEET:String = "triggerActionSheet";
		
		private var header:Header;
		private var menuBtn:Button;
		private var backBtn:Button;
		private var logoImg:Image;
		
		public function HeaderComponent()
		{
			
		}
		
		override protected function draw():void
		{
			var stageWidth:int = stage.stageWidth;
			var logoImgWidth:int = logoImg.width;
			var logoImgHeight:int = logoImg.height;
			
			header.width = stage.stageWidth;
			
			if(stageWidth < (logoImgWidth + 120))
			{
				var newWidth:int = stageWidth - 120;
				var scale:Number = newWidth/logoImgWidth;
				
				logoImg.width = newWidth;
				logoImg.height = logoImgHeight*scale;
			}
			
			super.draw();
		}
		
		override protected function initialize():void
		{
			menuBtn = new Button();
			menuBtn.nameList.add("inner-menu-btn");
			menuBtn.addEventListener(Event.TRIGGERED, onMenuBtnTrigger);
			
			backBtn = new Button();
			backBtn.nameList.add("back-btn");
			backBtn.addEventListener(Event.TRIGGERED, onBackBtnTrigger);
			
			header = new Header();
			logoImg = new Image(TextureAtlasManager.getAtlas().getTexture("speiuss"));
			header.leftItems = new <DisplayObject> [backBtn];
			header.centerItems = new <DisplayObject> [logoImg];
			header.rightItems = new <DisplayObject> [menuBtn];
			addChild(header);
			header.backgroundSkin = new Quad(10, 10, 0x353535);
			header.paddingRight = 20*CommonUtils.scaleValue;
			header.paddingLeft = 20*CommonUtils.scaleValue;
			
			super.initialize();
		}
		
		private function onBackBtnTrigger():void
		{
			this.dispatchEventWith(TRIGGER_BACK_BTN, true);
		}
		
		private function onMenuBtnTrigger():void
		{
			this.dispatchEventWith(TRIGGER_ACTIONSHEET, true);
		}		
		
	}
}